// ----------------------------------------------------------------楼栋下拉框JS-------------------------------------------------------------
//单元下拉框JS
$('#building').on('change', function () {
    var buildingId = $.form.selectSelects("building");
    $('#unit').empty();
    $('#tenantName').empty();
    $("#tenantName").find('option').remove();
    $('#tenantName').selectpicker('refresh');                   // 刷新
    $('#tenantName').selectpicker('render');                   // 渲染
    // 处理自己的业务
    $.ajax({
        url: prefix + "/addUnitList",    //请求的url地址
        dataType:"json",   //返回格式为json
        async:true,//请求是否异步，默认为异步，这也是ajax重要特性
        data:{"buildingId":buildingId},    //参数值
        type:"POST",   //请求方式
        success:function(data){
            //请求成功时处理
            if(data!=null){
                for(var i = 0;i < data.data.length; i++){
                    var unit = data.data[i];
                    $("#unit").append('<option value="'+unit.unitId+'">'+unit.unitName+'</option>');
                }
                $('#unit').selectpicker('refresh');                   // 刷新
                $('#unit').selectpicker('render');                   // 渲染
            }
        },
        error:function(){
            //请求出错处理
        }
    });
});

// ----------------------------------------------------------------单元下拉框JS-------------------------------------------------------------
//租户下拉框JS
$('#unit').on('changed.bs.select', function () {
    var unitIdStrArry = $('#unit').selectpicker('val');
    var unitIdStr = "";
    if(unitIdStrArry != null){
        unitIdStr = unitIdStrArry.toString();
    }
    $('#tenantName').empty();
    $.ajax({
        url: prefix + "/addTenantList",    //请求的url地址
        dataType:"json",   //返回格式为json
        async:true,//请求是否异步，默认为异步，这也是ajax重要特性
        data:{"unitIdStr":unitIdStr},    //参数值
        type:"POST",   //请求方式
        success:function(data){
            //请求成功时处理
            if(data.data!=null){
                for(var i = 0;i < data.data.length; i++){
                    var tenant = data.data[i];
                    $("#tenantName").append('<option value="'+tenant.tenantId+'">'+tenant.tenantName+'</option>');
                }
            }else {
                $("#tenantName").find('option').remove();
            }
            $('#tenantName').selectpicker('refresh');                   // 刷新
            $('#tenantName').selectpicker('render');                   // 渲染
        },
        error:function(){
            //请求出错处理
        }
    });
});

// ----------------------------------------------------------------租户下拉框JS-------------------------------------------------------------
//租户下拉框改变触发事件
$('#tenantName').on('changed.bs.select', function () {
    // 处理自己的业务
    var tenantId = $('#tenantName').selectpicker('val');
    $.ajax({
        url: prefix + "/addTenantAndExpense",    //请求的url地址
        dataType:"json",   //返回格式为json
        async:true,//请求是否异步，默认为异步，这也是ajax重要特性
        data:{"tenantId":tenantId},    //参数值
        type:"POST",   //请求方式
        success:function(data){
            //请求成功时处理
            var apportionRatio = 0;
            if(data.data.sysTenant!=null){
                var sysTenant = data.data.sysTenant;
                apportionRatioOver = data.data.apportionRatio;
                apportionRatio = (data.data.apportionRatio*100).toFixed(2)+'%';
                $("#tenantId").val(sysTenant.tenantId);
                $("#tenantArea").val(sysTenant.tenantArea);
                $("#monthlyRentFee").val(sysTenant.monthlyRentFee);
                $("#propertyFee").val(sysTenant.propertyFee);
                $("#apportionRatioTemp").val(apportionRatio);
                $("#apportionRatio").val(apportionRatioOver);
                if(sysTenant.tenantStatus == '0'){
                    $("#live").parent().removeClass("iradio-blue").addClass("iradio-blue checked");
                    $("#store").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                    $("#office").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                }else if(sysTenant.tenantStatus == '1'){
                    $("#store").parent().removeClass("iradio-blue").addClass("iradio-blue checked");
                    $("#live").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                    $("#office").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                }else {
                    $("#office").parent().removeClass("iradio-blue").addClass("iradio-blue checked");
                    $("#store").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                    $("#live").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                }
            }else {
                $("#tenantId").val("");
                $("#tenantArea").val("");
                $("#monthlyRentFee").val("");
                $("#propertyFee").val("");
                $("#apportionRatio").val("");
                $("#apportionRatioTemp").val(0);
                $("#live").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                $("#store").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                $("#office").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
            }
            if(data.data.sysExpense!=null){
                var sysExpense = data.data.sysExpense;
                $("#lastMonthEletableNumber").val(sysExpense.lastMonthEletableNumber);
                $("#lastMonthWatableNumber").val(sysExpense.lastMonthWatableNumber);
            }else {
                $("#lastMonthEletableNumber").val("");
                $("#lastMonthWatableNumber").val("");
            }
        },
        error:function(){
            //请求出错处理
        }
    });
});
//租户下拉框渲染触发事件
$('#tenantName').on('refreshed.bs.select', function () {
    // 处理自己的业务
    var tenantId = $('#tenantName').selectpicker('val');
    $.ajax({
        url: prefix + "/addTenantAndExpense",    //请求的url地址
        dataType:"json",   //返回格式为json
        async:true,//请求是否异步，默认为异步，这也是ajax重要特性
        data:{"tenantId":tenantId},    //参数值
        type:"POST",   //请求方式
        success:function(data){
            //请求成功时处理v
            var apportionRatio = 0;
            if(data.data.sysTenant!=null){
                var sysTenant = data.data.sysTenant;
                apportionRatioOver = data.data.apportionRatio;
                apportionRatio = (data.data.apportionRatio*100).toFixed(2)+'%';
                $("#tenantId").val(sysTenant.tenantId);
                $("#tenantArea").val(sysTenant.tenantArea);
                $("#monthlyRentFee").val(sysTenant.monthlyRentFee);
                $("#propertyFee").val(sysTenant.propertyFee);
                $("#apportionRatioTemp").val(apportionRatio);
                $("#apportionRatio").val(apportionRatioOver);
                if(sysTenant.tenantStatus == '0'){
                    $("#live").parent().removeClass("iradio-blue").addClass("iradio-blue checked");
                    $("#store").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                    $("#office").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                }else if(sysTenant.tenantStatus == '1'){
                    $("#store").parent().removeClass("iradio-blue").addClass("iradio-blue checked");
                    $("#live").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                    $("#office").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                }else {
                    $("#office").parent().removeClass("iradio-blue").addClass("iradio-blue checked");
                    $("#store").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                    $("#live").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                }
            }else {
                $("#tenantId").val("");
                $("#tenantArea").val("");
                $("#monthlyRentFee").val("");
                $("#propertyFee").val("");
                $("#apportionRatio").val("");
                $("#apportionRatioTemp").val(0);
                $("#live").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                $("#store").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
                $("#office").parent().removeClass("iradio-blue checked").addClass("iradio-blue");
            }
            if(data.data.sysExpense!=null){
                var sysExpense = data.data.sysExpense;
                $("#lastMonthEletableNumber").val(sysExpense.lastMonthEletableNumber);
                $("#lastMonthWatableNumber").val(sysExpense.lastMonthWatableNumber);
            }else {
                $("#lastMonthEletableNumber").val("");
                $("#lastMonthWatableNumber").val("");
            }
        },
        error:function(){
            //请求出错处理
        }
    });
});

/// ------------------------------------------------------------------租金监听事件-------------------------------------------------------------
//实收租金  监听
$("#realMonthlyRentFee").bind("input propertychange", function() {
    ninetyPercentRealMonthlyFee();
    totalMonthRealFee();
});
//实收物业  监听
$("#realPropertyFee").bind("input propertychange", function() {
    totalMonthRealFee();
});
//实收租金百分之90
function ninetyPercentRealMonthlyFee() {
    var ninetyPercentRealMonthlyFee = (parseFloat($("#realMonthlyRentFee").val())*0.9).toFixed(2);
    $("#ninetyPercentRealMonthlyFee").val(ninetyPercentRealMonthlyFee);
};
//宽带费  监听
$("#wlanFee").bind("input propertychange", function() {
    totalMonthRealFee();
});

// ------------------------------------------------------------------电费监听事件-------------------------------------------------------------
//上月电度数  监听
$("#lastMonthEletableNumber").bind("input propertychange", function() {
    //电费相关
    useElectricNumber();
    totalElectricNumber();
    useElectricFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//本月电度数  监听
$("#thisMonthEletableNumber").bind("input propertychange", function() {
    //电费相关
    useElectricNumber();
    totalElectricNumber();
    useElectricFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//电公摊基数  监听
$("#electricApportionBaseNumber").bind("input propertychange", function() {
    //电费相关
    apportionElectricNumber();
    totalElectricNumber();
    apportionElectricFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//电费单价  监听
$("#electricUninPrice").bind("input propertychange", function() {
    useElectricFee();
    apportionElectricFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//本月用电
function useElectricNumber() {
    var useElectricNumber = ($("#thisMonthEletableNumber").val()-$("#lastMonthEletableNumber").val()).toFixed(2);
    $("#useElectricNumber").val(useElectricNumber);
};
//公摊电度数
function apportionElectricNumber() {
    var apportionElectricNumber = parseInt($("#electricApportionBaseNumber").val()*apportionRatioOver).toFixed(2);
    $("#apportionElectricNumber").val(apportionElectricNumber);
};
//总用电度数
function totalElectricNumber() {
    var totalElectricNumber = (parseInt($("#useElectricNumber").val())+parseInt($("#apportionElectricNumber").val())).toFixed(2);
    $("#totalElectricNumber").val(totalElectricNumber);
};
//自用电费
function useElectricFee() {
    var useElectricFee = (parseFloat($("#useElectricNumber").val())*parseFloat($("#electricUninPrice").val())).toFixed(2);
    $("#useElectricFee").val(useElectricFee);
};
//公摊电费
function apportionElectricFee() {
    var apportionElectricFee = (parseFloat($("#apportionElectricNumber").val())*parseFloat($("#electricUninPrice").val())).toFixed(2);
    $("#apportionElectricFee").val(apportionElectricFee);
};

// -----------------------------------------------------------------水费监听事件-------------------------------------------------------------
//上月水表度数  监听
$("#lastMonthWatableNumber").bind("input propertychange", function() {
    //水费相关
    useWaterNumber()
    totalWaterNumber();
    useWaterFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//本月水表度数  监听
$("#thisMonthWatableNumber").bind("input propertychange", function() {
    //水费相关
    useWaterNumber();
    totalWaterNumber();
    useWaterFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//水公摊基数  监听
$("#waterApportionBaseNumber").bind("input propertychange", function() {
    //水费相关
    apportionWaterNumber();
    totalWaterNumber();
    apportionWaterFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//水费单价  监听
$("#waterUninPrice").bind("input propertychange", function() {
    //水费相关
    useWaterFee();
    apportionWaterFee();
    //合计相关
    totalUtilityFee();
    totalMonthRealFee();
    useTotalUtilityFee();
    apportionTotalUtilityFee();
});
//本月用水
function useWaterNumber() {
    var useWaterNumber = ($("#thisMonthWatableNumber").val()-$("#lastMonthWatableNumber").val()).toFixed(2);
    $("#useWaterNumber").val(useWaterNumber);
};
//公摊水度数
function apportionWaterNumber() {
    var apportionWaterNumber = parseInt($("#waterApportionBaseNumber").val()*apportionRatioOver).toFixed(2);
    $("#apportionWaterNumber").val(apportionWaterNumber);
};
//总用水度数
function totalWaterNumber() {
    var totalWaterNumber = (parseInt($("#useWaterNumber").val())+parseInt($("#apportionWaterNumber").val())).toFixed(2);
    $("#totalWaterNumber").val(totalWaterNumber);
};
//自用水费
function useWaterFee() {
    var useWaterFee = (parseFloat($("#useWaterNumber").val())*parseFloat($("#waterUninPrice").val())).toFixed(2);
    $("#useWaterFee").val(useWaterFee);
};
//公摊水费
function apportionWaterFee() {
    var apportionWaterFee = (parseFloat($("#apportionWaterNumber").val())*parseFloat($("#waterUninPrice").val())).toFixed(2);
    $("#apportionWaterFee").val(apportionWaterFee);
};
// -----------------------------------------------------------------合计费用监听事件-------------------------------------------------------------
//水电费合计
function totalUtilityFee() {
    var totalUtilityFee = (parseFloat($("#useElectricFee").val())+parseFloat($("#useWaterFee").val())+parseFloat($("#apportionElectricFee").val())+parseFloat($("#apportionWaterFee").val())).toFixed(2);
    $("#totalUtilityFee").val(totalUtilityFee);
};
//自用水电费合计
function useTotalUtilityFee() {
    var useTotalUtilityFee = (parseFloat($("#useElectricFee").val())+parseFloat($("#useWaterFee").val())).toFixed(2);
    $("#useTotalUtilityFee").val(useTotalUtilityFee);
};
//公摊水电费合计
function apportionTotalUtilityFee() {
    var apportionTotalUtilityFee = (parseFloat($("#apportionElectricFee").val())+parseFloat($("#apportionWaterFee").val())).toFixed(2);
    $("#apportionTotalUtilityFee").val(apportionTotalUtilityFee);
};
//本月实收金额合计
function totalMonthRealFee() {
    var totalMonthRealFee = (parseFloat($("#totalUtilityFee").val())+parseFloat($("#realPropertyFee").val()) +parseFloat($("#realMonthlyRentFee").val())+parseFloat($("#wlanFee").val())).toFixed(2);
    $("#totalMonthRealFee").val(totalMonthRealFee);
};
